package megalopolis.server;

import java.util.HashSet;
import java.util.Set;

import megalopolis.model.Building;
import megalopolis.model.Car;
import megalopolis.model.GameObject;
import megalopolis.model.Man;

/**
 * User: Kirill
 * Date: 18.03.2008
 */
public class WorldHelper {

	private World world;

	private static final int NEAR = 100;

	public WorldHelper(World world) {
		this.world = world;
	}

	public Set<GameObject> getGameObjects(Man playerMan) {
		Set<GameObject> ret = new HashSet<GameObject>();

		for (Man man : world.mans) {
			if (isNear(man, playerMan)) {
				ret.add(man);
			}
		}
		for (Car car : world.cars) {
			if (isNear(car, playerMan)) {
				ret.add(car);
			}
		}
		for (Building building : world.buildings) {
			if (isNear(building, playerMan)) {
				ret.add(building);
			}
		}

		return ret;
	}

	public void addMan(Man man) {
		world.mans.add(man);
	}

	private boolean isNear(GameObject obj1, GameObject obj2) {
		return (obj1.x - obj2.x) * (obj1.x - obj2.x) + (obj1.y - obj2.y) * (obj1.y - obj2.y) < NEAR * NEAR;
	}
}
